<?php
class MShopMember extends MY_Model {

	const STATE_DISABLE = 0;
	const STATE_ENABLE = 1;
	const STATE_DELETE = 2;
	protected $table = 'shop_member';
	private $filter = array();

    function __construct() {
        parent::__construct();
    }
    
    public static function getState($state=NULL){
    	$define = array(self::STATE_DISABLE=>'不可用',self::STATE_ENABLE=>'正常',self::STATE_DELETE=>'已注销');
    	if($state === NULL){
    		return $define;
    	}else{
    		return key_exists($state, $define)?$define[$state]:'';
    	}
    }
    
	public function getList($limit, $sort=false){
		$this->db->select( $this->table.'.*');
		$this->db->distinct();
		$this->db->from($this->table);
		$this->db->limit($limit['limit'],$limit['offset']);
		$this->formfilter->doFilter();
		if($this->_user->role_id != 1){
		    $this->db->where('parent_id',$this->_user->id);
		}
		if($sort){
			$this->db->order_by('sort', 'asc');
		}else{
			$this->db->order_by('id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	public function getCount(){
		$this->db->select('count(distinct g.id) as num');
		$this->db->from($this->table . ' as g');
		$this->formfilter->doFilter();
		if($this->_user->role_id != 1){
		  $this->db->where('parent_id',$this->_user->id);
		}
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	public function getById($id){
		$query = $this->db->where('id',$id)->get($this->table);
		return $query->row();
	}
	
	/**
	 * 得到所有组员 id=>name-qq
	 * @return NULL[]
	 * isAll = false 只显示当前组长下的组员 isAll = true 显示所有组员
	 */
	public function getAllMembers($isAll = FALSE){
	    $this->db->select('id,name,qq');
	    $this->db->from($this->table);
	    if($isAll === FALSE){
	       $this->db->where('parent_id',$this->_user->id);
	    }
	    $queryResult = $this->db->get()->result();
	    $kvArr = array();
	    foreach ($queryResult as $k=>$v){
	        $kvArr[$v->id] = $v->name.' — '.$v->qq;
	    }
	    return $kvArr;
	}
	
}
